/*
 * Copyright 2011 Marcos Fabio Pereira
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.moveme.examples.stepcounter;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import com.moveme.MoveMeClient;
import com.moveme.MoveMeListener;
import com.moveme.examples.gui.Messages;
import com.moveme.network.payload.PSPayload;
import com.moveme.network.payload.state.PSMoveState;
import com.moveme.utils.Float4;

/**
 * MoveMe Step counter.
 * 
 * @author Marcos Fabio Pereira
 * 
 */
public class Main extends JFrame implements MoveMeListener, ActionListener {

    /**
     * The MoveMeClient instance.
     */
    private MoveMeClient moveMeClient;

    /**
     * Ip Move.Me Server
     */
    private JTextField ipServer;

    /**
     * Create the panel.
     */
    public Main() {
        setMaximumSize(new Dimension(320, 640));
        setMinimumSize(new Dimension(320, 640));
        setSize(new Dimension(320, 640));
        setTitle("MoveMe Console");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setPreferredSize(new Dimension(320, 640));
        getContentPane().setLayout(null);

        JButton btnInitButton = new JButton("Init");
        btnInitButton.setActionCommand("init");
        btnInitButton.addActionListener(this);
        btnInitButton.setBounds(6, 64, 182, 23);
        getContentPane().add(btnInitButton);

        ipServer = new JTextField();
        ipServer.setBounds(6, 34, 182, 20);
        ipServer.setText("192.168.0.103");
        getContentPane().add(ipServer);
        ipServer.setColumns(10);

        ButtonGroup moveControllerGroup = new ButtonGroup();

        JRadioButton navController0RadioButton = new JRadioButton(
                "Nav controller 0");
        navController0RadioButton.setSelected(true);
        navController0RadioButton.setBounds(6, 131, 182, 23);
        navController0RadioButton.setActionCommand("navController0Selected");
        navController0RadioButton.addActionListener(this);
        getContentPane().add(navController0RadioButton);

        JRadioButton navController1RadioButton = new JRadioButton(
                "Nav controller 1");
        navController1RadioButton.setBounds(6, 157, 182, 23);
        navController1RadioButton.setActionCommand("navController1Selected");
        navController1RadioButton.addActionListener(this);
        getContentPane().add(navController1RadioButton);

        JRadioButton navController3RadioButton = new JRadioButton(
                "Nav controller 3");
        navController3RadioButton.setBounds(6, 209, 182, 23);
        navController3RadioButton.setActionCommand("navController3Selected");
        navController3RadioButton.addActionListener(this);
        getContentPane().add(navController3RadioButton);

        JRadioButton navController2RadioButton = new JRadioButton(
                "Nav controller 2");
        navController2RadioButton.setBounds(6, 183, 182, 23);
        navController2RadioButton.setActionCommand("navController2Selected");
        navController2RadioButton.addActionListener(this);
        getContentPane().add(navController2RadioButton);

        JRadioButton navController4RadioButton = new JRadioButton(
                "Nav controller 4");
        navController4RadioButton.setBounds(6, 235, 182, 23);
        navController4RadioButton.setActionCommand("navController4Selected");
        navController4RadioButton.addActionListener(this);
        getContentPane().add(navController4RadioButton);

        JRadioButton navController5RadioButton = new JRadioButton(
                "Nav controller 5");
        navController5RadioButton.setBounds(6, 261, 182, 23);
        navController5RadioButton.setActionCommand("navController5Selected");
        navController5RadioButton.addActionListener(this);
        getContentPane().add(navController5RadioButton);

        JRadioButton navController6RadioButton = new JRadioButton(
                "Nav controller 6");
        navController6RadioButton.setBounds(6, 287, 182, 23);
        navController6RadioButton.setActionCommand("navController6Selected");
        navController6RadioButton.addActionListener(this);
        getContentPane().add(navController6RadioButton);

        moveControllerGroup.add(navController0RadioButton);
        moveControllerGroup.add(navController1RadioButton);
        moveControllerGroup.add(navController2RadioButton);
        moveControllerGroup.add(navController3RadioButton);
        moveControllerGroup.add(navController4RadioButton);
        moveControllerGroup.add(navController5RadioButton);
        moveControllerGroup.add(navController6RadioButton);

        JLabel lblPsMoveServer = new JLabel("Choose the address"); //$NON-NLS-1$
        lblPsMoveServer.setBounds(6, 15, 182, 14);
        getContentPane().add(lblPsMoveServer);

        JLabel lblSelectAController = new JLabel(
                "Choose the navigation controller"); //$NON-NLS-1$
        lblSelectAController.setBounds(10, 110, 174, 14);
        getContentPane().add(lblSelectAController);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void notifyError(Throwable t) {
        System.out.println(t.getMessage());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void processData(PSPayload payload) {
        PSMoveState moveState = (PSMoveState) payload;

        // if (moveState.moveNavPadInfo[navControllerSelected].portStatus == 1)
        // {
        System.out.println("COMECO - STATUS DO NAVEGADOR "
                + navControllerSelected + " TAMANHO "
                + moveState.moveNavPadData[navControllerSelected].len);
        for (int j = 0; j < moveState.moveNavPadData[navControllerSelected].len; j++) {
            System.out
                    .println(j
                            + ":"
                            + moveState.moveNavPadData[navControllerSelected].button[j]);
        }
        System.out
                .println("FIM - STATUS DO NAVEGADOR " + navControllerSelected);
        // }
    }

    private boolean isChange = false;

    private int mNumberOfSteps = 0;

    private float mOldAccX;

    private float mOldAccY;

    private float mOldAccZ;

    private void processAccel(Float4 float4) {
        float xx = float4.x;
        float yy = float4.y;
        float zz = float4.z;

        float dot = (mOldAccX * xx) + (mOldAccY * yy) + (mOldAccZ * zz);
        float a = (float) Math.abs(Math.sqrt(mOldAccX * mOldAccX + mOldAccY
                * mOldAccY + mOldAccZ * mOldAccZ));

        float b = (float) Math.abs(Math.sqrt(xx * xx + yy * yy + zz * zz));

        dot /= (a * b);

        if (dot <= 0.994 && dot > 0.90) // bounce
        {

            if (!isChange) {

                isChange = true;
                mNumberOfSteps += 1;

            } else {
                isChange = false;
            }
        }

        mOldAccX = xx;
        mOldAccY = yy;
        mOldAccZ = zz;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void processImage(Image imageData) {
    }

    public static void main(String[] args) {
        Main moveMeConsole = new Main();
        moveMeConsole.pack();
        moveMeConsole.setVisible(true);
    }

    public JTextField getIpServer() {
        return ipServer;
    }

    private int navControllerSelected = 0;

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("init")) {
            try {
                System.out.println("Trying to connect to "
                        + this.getIpServer().getText());
                this.moveMeClient = new MoveMeClient(this.getIpServer()
                        .getText(), 7899, 5566, this);
                this.moveMeClient.init();
            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        } else if (e.getActionCommand().equals("navController0Selected")) {
            navControllerSelected = 0;
        } else if (e.getActionCommand().equals("navController1Selected")) {
            navControllerSelected = 1;
        } else if (e.getActionCommand().equals("navController2Selected")) {
            navControllerSelected = 2;
        } else if (e.getActionCommand().equals("navController3Selected")) {
            navControllerSelected = 3;
        } else if (e.getActionCommand().equals("navController4Selected")) {
            navControllerSelected = 4;
        } else if (e.getActionCommand().equals("navController5Selected")) {
            navControllerSelected = 5;
        } else if (e.getActionCommand().equals("navController6Selected")) {
            navControllerSelected = 6;
        }
    }
}